package com.mosai.alignpro;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.mosai.alignpro.view.DiagnosisView;

public class DiagnosisActivity extends BaseActivity {

    public static final int REQUEST_CODE_READ_EXTERNAL_STORAGE = 100;

    private View layoutDoctorInfo;
    private DiagnosisView diagnosisView;
    private Button btnEdit, btnConfirm, btnSave;
    private ImageView ivDoctorAvatar;
    private TextView tvDoctorName, tvDate, tvRemainder, tvBack, tvTitle;

    public static void startForResult(Activity activity, int requestCode) {
        if (activity == null) {
            return;
        }
        Intent intent = new Intent(activity, DiagnosisActivity.class);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diagnosis);
        initView();
        initData();
    }

    private void initView() {
        diagnosisView = findViewById(R.id.diagnosisView);
        btnEdit = findViewById(R.id.btnEdit);
        btnConfirm = findViewById(R.id.btnConfirm);
        btnSave = findViewById(R.id.btnSave);
        layoutDoctorInfo = findViewById(R.id.layoutDoctorInfo);
        ivDoctorAvatar = findViewById(R.id.ivDoctorAvatar);
        tvDoctorName = findViewById(R.id.tvDoctorName);
        tvDate = findViewById(R.id.tvDate);
        tvRemainder = findViewById(R.id.tvRemainder);
        tvBack = findViewById(R.id.tvBack);
        tvTitle = findViewById(R.id.tvTitle);
    }

    private void initData() {
        setDoctorAvatar("http://a0.att.hudong.com/16/12/01300535031999137270128786964.jpg");
        tvDoctorName.setText("Dr.Mike");
        tvDate.setText("2019.01.01 09:12 审核");

        tvRemainder.setText(getString(R.string.diagnosis_remainder, 1));

        tvRemainder.setVisibility(View.GONE);
        btnConfirm.setVisibility(View.GONE);
        layoutDoctorInfo.setVisibility(View.VISIBLE);
    }

    private void setDoctorAvatar(String uri) {
        int radius = getResources().getDimensionPixelSize(R.dimen.doctor_avatar_size) >> 1;
        RequestOptions options = new RequestOptions()
                .transform(new CenterCrop(), new RoundedCorners(radius));
        Glide.with(this)
                .load(uri)
                .placeholder(R.drawable.default_avatar)
                .error(R.drawable.default_avatar)
                .apply(options)
                .into(ivDoctorAvatar);
    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }

    private void refresh() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            diagnosisView.setHasEditPoint(true);
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_READ_EXTERNAL_STORAGE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE_READ_EXTERNAL_STORAGE && grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            refresh();
        }
    }

    public void onClickBack(View view) {
        finish();
    }

    public void onClickEdit(View view) {
        layoutDoctorInfo.setVisibility(View.GONE);
        tvRemainder.setVisibility(View.GONE);
        btnEdit.setVisibility(View.GONE);
        btnConfirm.setVisibility(View.GONE);
        btnSave.setVisibility(View.VISIBLE);
    }

    public void onClickConfirm(View view) {
    }

    public void onClickSave(View view) {
    }
}
